package threadpoolnio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ：xx
 * @date ：Created in 2020/2/27 19:06
 * @description：
 * @modified By：
 */
public class SocketThread extends Thread {
    private  Socket s=null;
    private int clientCount=0;
    private BufferedReader in=null;
    public SocketThread(Socket s,int clientCount){
        this.s=s;
        this.clientCount=clientCount;
        System.out.println("有新的客户端连接了：当前的连接数"+clientCount+",线程id:"+Thread.currentThread().getId());
    }

    @Override
    public void run() {
        try {
            in =new BufferedReader(new InputStreamReader(s.getInputStream(),"GBK"));
            String str = "";
            while ((str=in.readLine())!=null) {
                Date date = new Date();
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                String time = format.format(date);
                System.out.println(time + ":" + s.getRemoteSocketAddress() + ":" + str + ",客户端id："
                        + clientCount + "," + ",线程id:" + Thread.currentThread().getId()+"线程名字:"+Thread.currentThread().getName());
                
            }
            TCPServer_Thread.clientCount--;
            System.out.println(clientCount+"会话结束，线程id:" + Thread.currentThread().getId());
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
